//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    motto: 'Hello World',
    userInfo: {}
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    console.log('onLoad')
    var that = this
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      //更新数据
      that.setData({
        userInfo:userInfo
      })
    })
  },
  getValue(e) {
    this.setData({
      message: e.detail.value
    })
  },
  sendMessage() {
    wx.connectSocket({
      url: "ws://localhost:8080",
      data: {},
      // header: {}, // 设置请求的 header
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      success: function(res){
        // success
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
    const self = this;
    wx.onSocketOpen(function(res) {
      // callback
      console.log('websocket has been opened');
      wx.sendSocketMessage({
        data: self.data.message,
        success: function(res){
          // success
        },
        fail: function() {
          // fail
        },
        complete: function() {
          // complete
        }
      });
    });

    wx.onSocketMessage(function(data) {
      self.setData({
        message: data.data
      })
    });

    wx.onSocketClose(function() {
      // callback
      console.log('websocket has been closed');
    })
  }
})
